package com.homework.config;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/17 23:28
 */

@Configuration


@ComponentScan(basePackages = "com.homework")
@PropertySource(value = "classpath:db.properties")
@Slf4j
public class SpringConfig {
    private static final Logger log = LoggerFactory.getLogger(SpringConfig.class);
    @Value("${jdbc.driver}")
    private String driver ;
    @Value("${jdbc.url}")
    private String url ;
    @Value("${jdbc.username}")
    private String username ;
    @Value("${jdbc.password}")
    private String password ;

    @Bean
    public Connection getConnection() {
        try{
            Class.forName(this.driver) ;
            return DriverManager.getConnection(
                    this.url, this.username, this.password
            ) ;
        } catch (Throwable e) {
            log.error(e.getMessage());
        }
        return null ;
    }

}